package com.example.video

import android.media.PlaybackParams
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.widget.MediaController
import androidx.lifecycle.lifecycleScope
import kotlinx.android.synthetic.main.activity_main.*
import kotlinx.android.synthetic.main.activity_main.view.*
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch

class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        val videoPath ="android.resource://$packageName/${R.raw.video1}"
        videoView.setVideoPath(videoPath)
        //控制器
//        videoView.setMediaController(MediaController(this))
        //当video准备好
        videoView.setOnPreparedListener {
            progressBar.max = it.duration
            it.playbackParams = PlaybackParams().apply {
//                speed =2f  速度
//                pitch = 2f 音高
            }
            it.start()
        }
        lifecycleScope.launch {
            while (true){
                if(videoView.isPlaying){
                    progressBar.progress =videoView.currentPosition
                }
                delay(500)
            }
        }

    }
}